package io.framework.modules.water.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.water.dao.WaterNearshoreDao;
import io.framework.modules.water.dto.WaterNearshoreDTO;
import io.framework.modules.water.entity.WaterNearshoreEntity;
import io.framework.modules.water.service.WaterNearshoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 近岸海域海水
 *
 * @author Gujq email
 * @since 1.0.0 2020-10-15
 */
@Service
public class WaterNearshoreServiceImpl extends CrudServiceImpl<WaterNearshoreDao, WaterNearshoreEntity, WaterNearshoreDTO> implements WaterNearshoreService {

    @Override
    public QueryWrapper<WaterNearshoreEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<WaterNearshoreEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void deleteInfoByDate(String year) {
        baseDao.delete(new QueryWrapper<WaterNearshoreEntity>().eq("date_format(month,'%Y-%m')", year));
    }

    @Override
    public PageData<WaterNearshoreDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<WaterNearshoreEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(month,'%Y')", time);
        }
        if (page == null) page = -1;
        if (limit == null) limit = -1;
        Page<WaterNearshoreEntity> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<WaterNearshoreEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<WaterNearshoreDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), WaterNearshoreDTO.class);
        return new PageData<WaterNearshoreDTO>(dtoList, ip.getTotal());
    }

    @Override
    public Boolean checkData(String month) {
        Integer count = baseDao.checkData(month);
        return count > 0 ? true : false;
    }

    @Override
    public WaterNearshoreDTO getWaterInfoById(Map<String, Object> params) {
        WaterNearshoreEntity entity = baseDao.getWaterInfoById(params);
        return ConvertUtils.sourceToTarget(entity, WaterNearshoreDTO.class);
    }
}
